<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;
use App\Entity\SuanMingFenXi;
use App\Base\BaseModel;
use App\Setting\SuanMingFenXiSetting;

class SuanMingFenXiModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new SuanMingFenXiSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    /**
     * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
     */
    public function getRep()
    {
        $settings = new SuanMingFenXiSetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }

    /**
     * @return SuanMingFenXi[]
     */
    public function getAll()
    {
        return $this->getRep()->findBy(array(),array("id"=>"DESC"));
    }

    /**
     * @param $id
     * @return null|object|SuanMingFenXi
     */
    public function getById($id)
    {
        return $this->getRep()->find($id);
    }

    /**
     * @return array
     */
    public function getAllArray()
    {
        $data = $this->getAll();
        $re = [];
        foreach ($data as $one) {
            $re[] = $this->toArray($one);
        }
        return $re;
    }

    /**
     * @param $id
     * @return array
     */
    public function getByIdArray($id)
    {
        $en = $this->getById($id);
        return $en?$this->toArray($en):[];
    }


    /**
     * @param $key
     * @param $val
     * @return SuanMingFenXi
     */
    public function getByKV($key,$val)
    {
        $qb = $this->createDql();
        $qb->select("A")->from("App:SuanMingFenXi","A");
        $qb->where("A.$key = :val")->setParameter("val",$val);
        $query = $qb->getQuery();
        $data = $query->execute();
        if ($data) {
            return $data[0];
        }else{
            return [];
        }
    }

    /**
     * @param array $arr
     * @return SuanMingFenXi
     */
    public function getByKVs($arr =[])
    {
        $qb = $this->createDql();
        $qb->select("A")->from("App:SuanMingFenXi","A");
        $i = 0;
        foreach ($arr as $key=>$val) {
            if ($i == 0) {
                $qb->where("A.$key = :$key")->setParameter("$key",$val);
            }else{
                $qb->andWhere("A.$key = :$key")->setParameter("$key",$val);
            }
            $i++;
        }
        $qb->orderBy("A.orders","ASC");
        $query = $qb->getQuery();
        $data = $query->execute();
        if ($data) {
            return $data[0];
        }else{
            return [];
        }
    }

    /**
     * @param $key
     * @param $val
     * @return SuanMingFenXi[]
     */
    public function getArrayByKV($key,$val)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("A.*")->from("Suan_Ming_Fen_Xi","A");
        $qb->where("A.$key = :$key")->setParameter("$key",$val)->orderBy("orders","ASC");
        $data = $qb->execute()->fetchAll();
        return $data;
    }

    /**
     * @param array $arr
     * @return SuanMingFenXi[]
     */
    public function getArrayByKVs($arr)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("A.*")->from("SuanMingFenXi","A");
        $i = 0;
        foreach ($arr as $key=>$val) {
            if ($i == 0) {
                $qb->where("A.$key = :$key")->setParameter("$key",$val);
            }else{
                $qb->andWhere("A.$key = :$key")->setParameter("$key",$val);
            }
            $i++;
        }
        $data = $qb->execute()->fetchAll();
        return $data;
    }
}
